import { Controller, Post, Body, Get, Param, Delete, UseGuards, Request } from '@nestjs/common';
import { MessageService } from './message.service';
import { CreateMessageDTO } from 'src/dto/message/create-message.dto';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { AuthGuard } from 'src/core/auth/auth.guard';

@ApiTags('评论管理')
@Controller('message')
export class MessageController {
  constructor(private readonly messageService: MessageService) {}

  /**
   * 创建评论
   * @param request 请求对象
   * @param postId 文章ID
   * @param createMessageDTO 评论数据
   * @returns 创建的评论
   */
  @ApiOperation({ summary: '创建评论', description: '为指定文章创建一条评论' })
  @ApiBearerAuth()
  @ApiResponse({ status: 201, description: '评论创建成功' })
  @ApiResponse({ status: 401, description: '未授权' })
  @ApiResponse({ status: 404, description: '文章不存在或用户不存在' })
  @UseGuards(AuthGuard)
  @Post(':postId')
  async createMessage(
    @Request() req,
    @Param('postId') postId: number,
    @Body() createMessageDTO: CreateMessageDTO,
  ) {
    const userId = req.user.id;
    return this.messageService.createMessage(postId, userId, createMessageDTO);
  }

  /**
   * 获取文章的所有评论
   * @param postId 文章ID
   * @returns 评论列表
   */
  @ApiOperation({
    summary: '获取文章评论',
    description: '获取指定文章的所有评论',
  })
  @ApiParam({ name: 'postId', description: '文章ID' })
  @ApiResponse({ status: 200, description: '成功获取评论列表' })
  @ApiResponse({ status: 404, description: '文章不存在' })
  @Get('post/:postId')
  async getMessagesByPostId(@Param('postId') postId: number) {
    return this.messageService.getMessagesByPostId(postId);
  }

  /**
   * 删除评论
   * @param request 请求对象
   * @param messageId 评论ID
   * @returns 删除结果
   */
  @ApiOperation({ summary: '删除评论', description: '删除指定的评论' })
  @ApiBearerAuth()
  @ApiParam({ name: 'messageId', description: '评论ID' })
  @ApiResponse({ status: 200, description: '评论删除成功' })
  @ApiResponse({ status: 401, description: '未授权' })
  @ApiResponse({ status: 404, description: '评论不存在或无权限删除' })
  @UseGuards(AuthGuard)
  @Delete(':messageId')
  async deleteMessage(@Request() req, @Param('messageId') messageId: number) {
    const userId = req.user.id;
    return this.messageService.deleteMessage(messageId, userId);
  }
}